package cn.sstir.hackathon.controller;

import cn.sgst.tool.common.response.Response;
import cn.sgst.tool.common.util.ToolUtil;
import cn.sstir.hackathon.core.ExportType;
import cn.sstir.hackathon.core.dict.CountryDict;
import cn.sstir.hackathon.core.http.ClientService;
import cn.sstir.hackathon.core.page.PageResult;
import cn.sstir.hackathon.core.search.PatentQuery;
import cn.sstir.hackathon.service.PatentService;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: fli
 * @email: fli@sstir.cn
 * @date: 2020/1/16 16:44
 */
@Controller
@RequestMapping("/patent")
public class PatentController {

    @Autowired
    private PatentService patentService;

    @Autowired
    private ClientService clientService;



    /**
     * 搜索
     *
     * @author fli
     * @date 2020/1/17 0:52
     */
    @GetMapping("/search")
    @ResponseBody
    public Response search(PatentQuery patentQuery) throws Exception{
        PageResult pageResult = patentService.search(patentQuery);
        return Response.success(pageResult);
    }


    /**
     * 导出excel
     *
     * @author fli
     * @date 2020/1/17 0:52
     */
    @GetMapping("/export/excel")
    public void exportExcel(HttpServletResponse response, @RequestParam List<String> columns,PatentQuery patentQuery) throws Exception{
            patentService.export(response,columns,patentQuery,ExportType.excel);
    }


    /**
     * 导出csv
     *
     * @author fli
     * @date 2020/1/17 0:52
     */
    @GetMapping("/export/csv")
    public void exportCsv(HttpServletResponse response, @RequestParam List<String> columns,PatentQuery patentQuery) throws Exception{
        patentService.export(response,columns,patentQuery,ExportType.csv);
    }

    /**
     * 导出csv
     *
     * @author fli
     * @date 2020/1/17 0:52
     */
    @GetMapping("/export/tsv")
    public void exportTsv(HttpServletResponse response, @RequestParam List<String> columns,PatentQuery patentQuery) throws Exception{
        patentService.export(response,columns,patentQuery,ExportType.tsv);
    }

    @RequestMapping("/patentSearch")
    @ResponseBody
    public Map<String,String> patentSearch(String keyword) throws Exception{
        String url = "";
        if(ToolUtil.isEmpty(keyword)){
             url = "http://10.166.166.215:1234/patent";
        }else{
             url = "http://10.166.166.215:1234/patent?keyword="+keyword;
        }
        String result =  clientService.doGet(url);
        List<Map> list = new ArrayList<>();
        List<Map> list1 = new ArrayList<>();
        List<Map> list2 = new ArrayList<>();
        JSONObject jsObject =   JSONObject.parseObject(result);
        if(ToolUtil.isNotEmpty(jsObject)){
           JSONArray jsonArray = jsObject.getJSONArray("country");
            list = JSONObject.parseArray(jsonArray.toJSONString(), Map.class);
           if(ToolUtil.isNotEmpty(list)){
               for(Map map:list){
                   map.put("key",CountryDict.fromCode((String) map.get("key")));
               }
           }

            JSONArray jsonArray1 = jsObject.getJSONArray("applicants");
            list1 = JSONObject.parseArray(jsonArray1.toJSONString(), Map.class);
            if(ToolUtil.isNotEmpty(list1)){
                for(Map map:list1){
                    map.put("key", map.get("key"));
                }
            }

            JSONArray jsonArray2 = jsObject.getJSONArray("patent_type");
            list2 = JSONObject.parseArray(jsonArray2.toJSONString(), Map.class);
            if(ToolUtil.isNotEmpty(list1)){
                for(Map map:list2){
                    map.put("key", map.get("key"));
                }
            }
        }
        String str = JSONObject.toJSONString(list);
        String str1 = JSONObject.toJSONString(list1);
        String str2 = JSONObject.toJSONString(list2);
        Map<String,String> maps = new HashMap<>();
        maps.put("type",str2);
        maps.put("applicants",str1);
        maps.put("country",str);
        return  maps;
    }
}
